package com.nrgedge.magnolia.form.processors;

import info.magnolia.cms.core.Content;
import info.magnolia.context.MgnlContext;
import info.magnolia.module.form.processors.AbstractFormProcessor;
import info.magnolia.module.form.processors.FormProcessorFailedException;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.nrgedge.magnolia.jcaptcha.JCaptchaServlet;

public class JCaptchaFormProcessor extends AbstractFormProcessor {

	@Override
	protected void internalProcess(Content content, Map<String, Object> parameters) throws FormProcessorFailedException {
		final HttpServletRequest request = MgnlContext.getWebContext().getRequest();

		boolean isResponseCorrect = false;
		// retrieve the response
		String response = request.getParameter("j_captcha_response");

		isResponseCorrect = JCaptchaServlet.validateResponse(request, response);

		if (!isResponseCorrect) {
			throw new FormProcessorFailedException("JCaptchaFormProcessor.errorMessage");
		}
	}

}
